package com.msos.service.PrepaidRechargeService;

import com.msos.dao.prepaidRecharge.PrepaidRechargeDao;
import com.msos.model.ReCharge.ReCharge;
import com.msos.model.customer.Customer;
import com.msos.utils.CommonsUtils;
import com.msos.utils.DBUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.sql.Connection;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 弟中弟：小邦
 */
public class PrepaidRechargeService {

    PrepaidRechargeDao prepaidRechargeDao = new PrepaidRechargeDao();

    //帮别人充值
    public Integer prepaidRecharge2(String phone,String nextPhone,String cardNumber,String password,Double money){

        Connection con = DBUtils.getCon();

        try {
            //先查询被充值手机号是否存在
            Customer customer = prepaidRechargeDao.getCustomerByPhone(phone);
            if(customer != null){
                //表示被充值手机用户存在
                if(nextPhone.equals(phone)){
                    //前后手机号输入无误
                    if("移动".equals(customer.getCardType())){
                        //移动卡
                        //查询充值卡号是否存在
                        ReCharge recharge = prepaidRechargeDao.getRechargeByCardNumber(cardNumber);
                        if(recharge != null){
                            //充值卡存在
                            if(recharge.getEnabled()){
                                //可使用状态
                                //创建一个简单的日期格式
                                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                                //获取当前年月日
                                String dateNow = sdf.format(new Date());
                                System.out.println(dateNow);
                                //获取卡号启动日期
                                String startDate = recharge.getStartDate();
                                System.out.println(startDate);
                                //获取卡号结束日期
                                String endDate = recharge.getEndDate();
                                System.out.println(endDate);
                                //判断日期是否可用
                                if(dateNow.compareTo(startDate) >= 0 && dateNow.compareTo(endDate) <= 0){
                                    //充值卡使用期有效
                                    System.out.println(dateNow.compareTo(startDate));
                                    System.out.println(dateNow.compareTo(endDate));
                                    if(password.equals(recharge.getPassword())){
                                        //密码正确
                                        //开启事务
                                        con.setAutoCommit(false);
                                        //进行充值
                                        prepaidRechargeDao.AddMoneyByPhone(money,phone);
                                        //调整充卡号状态
                                        Byte enabled = 0;
                                        prepaidRechargeDao.updateCardByNumber(enabled,cardNumber);
                                        con.commit();
                                        return CommonsUtils.RECHARGE_SUCCESS;
                                    }else {
                                        //充值卡号密码错误
                                        return CommonsUtils.CARD_PASSWORD_ERROR;
                                    }
                                }else {
                                    //充值卡过期
                                    return CommonsUtils.CARD_TIMEOUT;
                                }
                            }else {
                                //充值卡已被使用
                                return CommonsUtils.CARD_IS_USE;
                            }
                        }else {
                            //充值卡不存在
                            return CommonsUtils.CARD_IS_NULL;
                        }
                    }else {
                        //不是移动卡
                        return CommonsUtils.TYPE_OF_ERROR;
                    }
                }else {
                    //前后手机号有误
                    return CommonsUtils.inconformity;
                }
            }else {
                //手机用户不存在
                return CommonsUtils.USER_NOTFOUND_OR_PASSWORD_BAD;
            }
        } catch (SQLException throwables) {
            try {
                con.rollback();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }finally {
            DBUtils.close(con);
        }
        return CommonsUtils.EXCEPTION_ERROR;
    }

    //自己充值
    public Integer prepaidRecharge1(String cardNumber, String password, Double money, HttpServletRequest req){

        Connection con = DBUtils.getCon();

        //获取session
        HttpSession session = req.getSession();
        //根据session获取当前登录用户的手机号
        Customer customer = (Customer) session.getAttribute("loginCustomer");


        try {
            //查询充值卡号是否存在
            ReCharge recharge = prepaidRechargeDao.getRechargeByCardNumber(cardNumber);
            if(recharge != null){
                //充值卡存在
                if(recharge.getEnabled()){
                    //可使用状态
                    //创建一个简单的日期格式
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                    //获取当前年月日
                    String dateNow = sdf.format(new Date());
                    System.out.println(dateNow);
                    //获取卡号启动日期
                    String startDate = recharge.getStartDate();
                    System.out.println(startDate);
                    //获取卡号结束日期
                    String endDate = recharge.getEndDate();
                    System.out.println(endDate);
                    //判断日期是否可用
                    if(dateNow.compareTo(startDate) >= 0 && dateNow.compareTo(endDate) <= 0){
                        //充值卡使用期有效
                        System.out.println(dateNow.compareTo(startDate));
                        System.out.println(dateNow.compareTo(endDate));
                        if(password.equals(recharge.getPassword())){
                            //密码正确
                            con.setAutoCommit(false);
                            //进行充值
                            prepaidRechargeDao.AddMoneyByPhone(money,customer.getPhone());
                            //调整充卡号状态
                            Byte enabled = 0;
                            prepaidRechargeDao.updateCardByNumber(enabled,cardNumber);
                            con.commit();
                            return CommonsUtils.RECHARGE_SUCCESS;
                        }else {
                            //充值卡号密码错误
                            return CommonsUtils.CARD_PASSWORD_ERROR;
                        }
                    }else {
                        //充值卡过期
                        return CommonsUtils.CARD_TIMEOUT;
                    }
                }else {
                    //充值卡已被使用
                    return CommonsUtils.CARD_IS_USE;
                }
            }else {
                //充值卡不存在
                return CommonsUtils.CARD_IS_NULL;
            }
        } catch (SQLException throwables) {
            try {
                con.rollback();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }finally {
            DBUtils.close(con);
        }
        return CommonsUtils.EXCEPTION_ERROR;
    }

}
